<?php
date_default_timezone_set('Asia/Shanghai');//时区配置
$conn=mysql_connect("121.37.61.76","YuanChengG","idcnYancq4");
include 'functions.php';
mysql_select_db("new_gsdai", $conn);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);

$time1 = strtotime(date('2014-01-03 00:00:00'));
$sql = "SELECT user_id,sum(repay_yesaccount) as repayyes FROM ych_borrow_collection WHERE repay_yestime>$time1 group by user_id";
$result = db_fetch_arrays($sql, $conn);
$index = 0;
foreach ($result as $key => $value){
	$mode = $value['user_id']%100;
	$sql1 = "SELECT sum(money) as my FROM ych_account_log_$mode WHERE add_time>=$time1 and ftype=17 and user_id=".$value['user_id'];
	$result1 = db_fetch_array($sql1, $conn);
	if($value['repayyes'] != $result1['my']){
		$money = $result1['my']-$value['repayyes'];
		$account_result_reward 	= db_fetch_array("select * from ych_account where user_id={$value['user_id']}", $conn);
		$log['user_id'] 		= $value['user_id'];
		$log['ftype'] 			= 51;
		$log['money'] 			= $money;
		$log['total'] 			= $account_result_reward['total'];
		$log['use_money'] 		= $account_result_reward['use_money']-$log['money'];
		$log['no_use_money'] 	= $account_result_reward['no_use_money'];
		$log['collection'] 		= $account_result_reward['collection']+$log['money'];
		$log['to_user'] 		= "1";
		$log['remark'] 			= "管理员对重复还款的回滚";
		$log['add_time'] 		= time();
		$log['addip'] 			= '127.0.0.1';
		$mode = $value['user_id']%100;
		$sql  ="insert into ych_account_log_$mode(user_id,ftype,total,money,use_money,no_use_money,
														collection,to_user,remark,add_time,addip) values( 
					 {$value['user_id']},{$log['ftype']},{$log['total']},{$log['money']},
					 {$log['use_money']},{$log['no_use_money']},
					 {$log['collection']},{$log['to_user']},'{$log['remark']}',
					 {$log['add_time']},'{$log['addip']}'
					 )";
		$query = mysql_query($sql, $conn);
		if(!$query){
			echo $sql;
			exit;
		}
		$query = mysql_query("update ych_account set collection=collection+{$log['money']},
					 use_money=use_money-{$log['money']} where user_id={$value['user_id']}", $conn);
		if(!$query){
			echo $sql;
			exit;
		}
		echo $value['user_id'].'='.$money.';';
		$index++;
	}
	
	
	
}
